package popmail
{	
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.events.ProgressEvent;
	import flash.events.SecurityErrorEvent;
	import flash.net.Socket;
	import flash.utils.ByteArray;
	import flash.utils.Endian;
	import flash.utils.IDataInput;
	
	import mx.utils.StringUtil;

	public class Pop3Mail extends EventDispatcher
	{
		
		private var _host:String = "";
		private var _port:int = 0;
		private var _userid:String = "";
		private var _pass:String = "";
		
		private var sock:Socket;
		
		private var _isLogin:Boolean = false;
		
		private var _cmd:String = "";
		
		public function get cmd():String
		{
			return _cmd;
		}
		
		public function Pop3Mail($host:String,$port:int, $id:String, $pass:String)
		{
			_host = $host;
			_port = $port;
			_userid = $id;
			_pass = $pass;
		}
		
		public function startPop3():void
		{
			sock = new Socket(_host, _port);
			sock.endian = Endian.BIG_ENDIAN;
			sock.addEventListener(Event.CONNECT, socketConnectHandler);
			sock.addEventListener(SecurityErrorEvent.SECURITY_ERROR, socketsecureErrorHandler);
		}
		
		protected function socketConnectHandler(e:Event):void
		{
			trace("socketConnectHandler ================");
			var rst:String = sock.readUTFBytes(sock.bytesAvailable);
			sock.writeUTFBytes("USER "+_userid+"\n");
			sock.writeUTFBytes("PASS "+_pass+"\n");
			sock.flush();
			dispatchEvent(new Pop3Events(Pop3Events.POP3_CONNECT_COMPLETE));
		}
		
		protected function socketdataProgressHandler(e:ProgressEvent):void
		{
			var event:Pop3Events;
			
			//trace(sock.readUTFBytes(sock.bytesAvailable));
			
			if(_cmd == "TOP")
			{
				//var input:IDataInput = sock.bytesAvailable;
				var _line:ByteArray = new ByteArray;
				var b:int;
				var arr:Array = new Array;
				while(sock.bytesAvailable)
				{
					b = sock.readByte();
					_line.writeByte(b);
					if(b == 0x0A)
					{
						
						_line.position = 0;
						var newline:ByteArray = new ByteArray;
						_line.readBytes(newline, 0, _line.bytesAvailable);
						newline.position = 0;
						_line.clear();
						_line.position = 0;		
						
						var str:String = StringUtil.trim(newline.readUTFBytes(newline.bytesAvailable));

						//if(str.indexOf("Subject:") != -1)
							//trace("subject ==",str);
						
						
						if(StringUtil.trim(str).indexOf("Content-Type:") == 0)
						{
							arr.push(str);
							e.preventDefault();
							sock.removeEventListener(ProgressEvent.SOCKET_DATA, socketdataProgressHandler);
							event = new Pop3Events(Pop3Events.POP3_DATA_COMPLETE);
							event.result = arr;
							dispatchEvent(event);
							break;
						}
						
						arr.push(str);
						
					}
					
					
				}
			}
			else
			{
				var rst:String = sock.readUTFBytes(sock.bytesAvailable);
				//trace("socketdataProgressHandler =======================");
				event = new Pop3Events(Pop3Events.POP3_DATA_COMPLETE);
				event.result = rst;
				dispatchEvent(event);
				//trace("result end ======================================");
			}
		}
		
		protected function socketsecureErrorHandler(e:SecurityErrorEvent):void
		{
			trace("socketsecureErrorHandler");
		}
		
		public function sendcommand($value:String):void
		{
			_cmd = $value.split(" ")[0];
			sock.addEventListener(ProgressEvent.SOCKET_DATA, socketdataProgressHandler);
			sock.writeUTFBytes($value+"\n");
			sock.flush();
		}
		
		
	}
}